package com.ghost.manager
{
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.adobe.cairngorm.control.CairngormEventDispatcher;
	import com.ghost.model.Global_data;
	import com.ghost.model.Global_ui_data;
	import com.ghost.utils.BrowserCanvas;
	import com.ghost.utils.Debug;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	
	public class Js_manager
	{
		private static var inst:Js_manager ;
		public static function getInstance():Js_manager
		{
			if(inst == null)
			{
				inst = new Js_manager();
			}
			return inst ;
		}
		public var canvas:BrowserCanvas;
		
		public function Js_manager()
		{
		}
		/**
		 * @param s 舞台容器
		 * @param w 正常宽
		 * @param h 正常高
		 * @param Max_width 宽最大值
		 * @param Max_height 高最大值
		 * @param Min_width 宽最小值
		 * @param Min_height 高最小值
		 * 
		 */		
		public function ori(s:*,w:Number,h:Number,Max_width:Number,Max_height:Number,Min_width:Number,Min_height:Number):void
		{
			canvas = new BrowserCanvas(s);
			if(w>Max_width)w=Max_width;
			if(h>Max_height)h=Max_height;
			if(w<Min_width)w=Min_width;
			if(h<Min_height)h=Min_height;
			
			Global_data.stage_s_width = w;
			Global_data.stage_s_height = h;
			canvas.width = String(w);
			canvas.height = String(h);
			canvas.maxWidth = String(Max_width);
			canvas.maxHeight = String(Max_height);
			canvas.minWidth = String(Min_width);
			canvas.minHeight = String(Min_height);
		}
		/* public function init():void
		{
			if(ExternalInterface.available)
			{
     			//注册js的回调函数,'flashFunc'是注册的函数名,func是对应的执行函数.
    		 	ExternalInterface.addCallback('resize_Handler', resize_Handler);
	 		}
		}
		private function resize_Handler():void
		{
			Debug.log(new Array(resize_obj,resize_h,resize_w,resize_obj.parent.x,Global_data.Game_s.stageWidth,Global_data.Game_s.stageHeight,Global_data.Game_s.x,Global_data.Game_s.y));
			resize_Stage(resize_obj1,resize_obj,resize_w,resize_h,resize_ld_bool);
		}
		public function call_js_resize(width:Number,height:Number):void
		{
			ExternalInterface.call("resizeSWF",width,height);
		} 
		在没有声明DOCTYPE的IE中，浏览器显示窗口大小只能以下获取：
		?
		1
		2
		
		document.body.offsetWidth
		document.body.offsetHeight
		
		在声明了DOCTYPE的浏览器中，可以用以下来获取浏览器显示窗口大小：
		?
		document.documentElement.clientWidth
		document.documentElement.clientHeight
		
		IE，FF，Safari皆支持该方法，opera虽支持该属性，但是返回的是页面尺寸；
		
		同时，除了IE以外的所有浏览器都将此信息保存在window对象中，可以用以下获取：
		?
		window.innerWidth
		window.innerHeight
		*/
		//w:舞台的宽 h：舞台的高 真正的stage的宽和高没有变
		public function resize_Stage(w:Number,h:Number):void
		{
			canvas.width = String(w);
			canvas.height = String(h);
			Global_data.stage_s_width = w;
			Global_data.stage_s_height = h;
			//CairngormEventDispatcher.getInstance().dispatchEvent(new CairngormEvent("change"));
		}
		public function resize_container(obj:DisplayObject,w:Number,h:Number):void
		{
			obj.width = w;
			obj.height = h;
		}
		//改变mc的位置(own_bool:true改变自己的坐标 false 改变所有子容器的坐标) type_num:0：原始的 1放大的
		public function change_center_p(mc:MovieClip,own_bool:Boolean=true,type_num:int=0):void
		{
			var c_w:Number = (type_num=1?(Global_data.stage_s_width-Global_data.stage_r_width):(Global_data.stage_r_width-Global_data.stage_s_width));
			var c_h:Number = (type_num=1?(Global_data.stage_s_height-Global_data.stage_r_height):(Global_data.stage_r_height-Global_data.stage_s_height));
			if(own_bool)
			{
				if(type_num==0)
				{
					mc.x = 0;
					mc.y = 0;
				}
				else
				{
					mc.x = (mc.x-c_w)/2;
					mc.y = (mc.y-c_h)/2;
				}
			}
			else
			{
				var child_num:int = mc.numChildren;
				if(type_num==0)
				{
					init(mc,child_num,c_w,c_h,1);
				}
				else
				{
					init(mc,child_num,c_w,c_h,-1);
				}
				
			}
		}
		private function init(mc:MovieClip,n:int,c_w:Number,c_h:Number,b:int):void
		{
			for(var i:int = 0;i<n;i++)
			{
				mc.getChildAt(i).x = (mc.getChildAt(i).x+c_w*b)/2;
				mc.getChildAt(i).y = (mc.getChildAt(i).y+c_h*b)/2;
			}
		}

	}
}